import java.util.Map;

public class BasicAI implements AI {
  public Action decide(Battle game_state) {
    //extra {}'s here for no reason
    {
      Combatant c = game_state.movable_set().iterator().next();
      Main.log.println("AI is moving: "+c.name());
      Map<MapLocation,Path> move_range = game_state.move_range(c);
      double best_priority = 0.0;
      MapLocation best_destination = null;
      Combatant best_target = null;
      for(MapLocation destination: move_range.keySet()) {
        if(game_state.at(destination) != null && game_state.at(destination) != c)
          continue;
        Combatant target = null;
        double target_priority = 0.0;
        double destination_priority = 0.0;
        for(Combatant enemy: game_state)
          if(game_state.team(c).is_enemy_of(game_state.team(enemy))) {
            destination_priority -= game_state.distance(destination,game_state.loc(enemy));
            if(c.weapon().within_range(destination,game_state.loc(enemy))) {
              Battle temp_battle = game_state.clone();
              temp_battle.move(c,destination);
              Combatant.CombatInfo my_info = new Combatant.CombatInfo(temp_battle,c,enemy);
              Combatant.CombatInfo enemy_info = new Combatant.CombatInfo(temp_battle,enemy,c);
              double my_effectiveness = 100.0*my_info.expected_damage() / enemy.hp();
              double enemy_effectiveness = 30.0*enemy_info.expected_damage() / c.hp();
              double curr_target_priority = my_effectiveness - enemy_effectiveness;
              Main.debug.println("  Considering attacking "+enemy.name()+": "+my_effectiveness+" - "+enemy_effectiveness);
              Main.debug.println("    "+my_info+". "+enemy_info);
              if(curr_target_priority > target_priority) {
                target_priority = curr_target_priority;
                target = enemy;
              }
            }
          }
        double priority = destination_priority + target_priority;
        if(best_destination == null || priority > best_priority) {
          Main.debug.println("  Best destination = "+destination+" with priority = "+priority);
          best_destination = destination;
          best_priority = priority;
          best_target = target;
        } else {
          Main.debug.println("  Igoring potential destination = "+destination+" with priority = "+priority);
        }
      }
      Main.debug.println("  Moving to "+best_destination+" ...");
      Action result;
      if(best_target != null)
        result = Action.attack(c,best_destination,best_target);
      else
        result = Action.wait(c,best_destination);
      return result;
    }
  }
}
